﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Jurvis
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            //this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
            //this.TopMost = true;
            ScanCardLibary.LogInfoOut += ShowLog;
            InitializeComponent();
        }
       
        void ShowLog(string str)
        {
            if (InvokeRequired)
            {
                Invoke(new DelegateString(ShowLog), new object[] { str });
            }
            else
            {
                TB_Log.AppendText(str);
                //listBoxRecordData.Items.Add(strData);
            }
        }


        private const int WM_HOTKEY = 0x312; //窗口消息：热键
        private const int WM_CREATE = 0x1; //窗口消息：创建
        private const int WM_DESTROY = 0x2; //窗口消息：销毁

        private const int HotKeyID_Start = 1; //热键ID（自定义）
        private const int HotKeyID_Stop = 2; //热键ID（自定义）

        protected override void WndProc(ref Message msg)
        {
            base.WndProc(ref msg);
            switch (msg.Msg)
            {
                case WM_HOTKEY: //窗口消息：热键
                    int tmpWParam = msg.WParam.ToInt32();
                    if (tmpWParam == HotKeyID_Start)
                    {
                        StartScanCardLibary();
                        //MessageBox.Show("Statr");
                    }
                    else if (tmpWParam == HotKeyID_Stop)
                    {
                        StopScanCardLibary();
                        //MessageBox.Show("Stop");
                    }
                    break;
                case WM_CREATE: //窗口消息：创建
                    HotKey.RegHotKey(this.Handle, HotKeyID_Start, HotKey.KeyModifiers.None, Keys.F10);
                    HotKey.RegHotKey(this.Handle, HotKeyID_Stop, HotKey.KeyModifiers.None, Keys.F11);
                    break;
                case WM_DESTROY: //窗口消息：销毁
                    HotKey.UnRegHotKey(this.Handle, HotKeyID_Start); //销毁热键
                    HotKey.UnRegHotKey(this.Handle, HotKeyID_Stop); //销毁热键
                    break;
                default:
                    break;
            }
        }

        Thread threadScanCardLib = null;
        void StartScanCardLibary()
        {
            if (threadScanCardLib == null)
            {
                threadScanCardLib = new Thread(new ThreadStart(ScanCardLibary.Start));
                threadScanCardLib.IsBackground = true;
                threadScanCardLib.Start();
            }
            else if (threadScanCardLib.IsAlive == false)
            {
                threadScanCardLib = new Thread(new ThreadStart(ScanCardLibary.Start));
                threadScanCardLib.IsBackground = true;
                threadScanCardLib.Start();
            }
            
        }

        void StopScanCardLibary()
        {
            if (threadScanCardLib.IsAlive == true)
            {
                threadScanCardLib.Abort();
                ScanCardLibary.Stop();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.TopMost = false;

            this.BringToFront();

            this.TopMost = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Interval = 100;
            timer1.Start();
            OCR ocr = new OCR();

            string str = ocr.OCR_Do("684.bmp");
            TB_Log.AppendText(str);
            TB_Log.AppendText("\r\n end");
        }
    }
}
